<?php
/**
 * Created by PhpStorm.
 * User: CJJ
 * Date: 2017/8/24
 * Time: 10:09
 */

require_once ('../../SQLBase.php');

class HotelMoneyManger{
    /*
    * 获取油价数据
    */
    function getOilData(){
        $db=new SQLBase();
        $queryStr='SELECT * FROM oil_fee;';
        // 	echo $queryStr;
        $result=$db->queryData($queryStr);
        if(!$result){
            $resultArrayAll['state']=-1;
        }else{
            $resultArrayAll['state']=1;
            $resultArrayAll['data']=$result->fetch_assoc();
        }
        return $resultArrayAll;
    }
    /*
     * 更新油价
     */
    function updateOil($data){
        $db=new SQLBase();
        $queryStr='UPDATE oil_fee SET price='.$data['price'].',count='.$data['count'].';';
        if($db->queryData($queryStr))
            $resultArryAll['state']=1;
        else
            $resultArryAll['state']=-1;
        return $resultArryAll;
    }
    /*
     * 获取公杂费数据
     */
    function getPublicExpenseData(){
        $db=new SQLBase();
        $queryStr='SELECT * FROM public_expense;';
        // 	echo $queryStr;
        $result=$db->queryData($queryStr);
        if(!$result){
            $resultArrayAll['state']=-1;
        }else{
            $resultArrayAll['state']=1;
            $resultArrayAll['data']=$result->fetch_assoc();
        }
        return $resultArrayAll;
    }
    /*
     * 更新公杂费
     */
    function updatePublicExpenseData($data){
        $db=new SQLBase();
        $queryStr='UPDATE public_expense SET ';
        $queryStr.='local_food_subsidy='.$data['local_food_subsidy'];
        $queryStr.=',local_public_expense='.$data['local_public_expense'];
        $queryStr.=',province_food_subsidy='.$data['province_food_subsidy'];
        $queryStr.=',outprovince_food_subsidy='.$data['outprovince_food_subsidy'];
        $queryStr.=',province_public_expense='.$data['province_public_expense'];
        $queryStr.=',outprovice_public_expense='.$data['outprovice_public_expense'].';';
        // 	echo  $querStr;
        if($db->queryData($queryStr)){
            $resultArrayAll['state']=1;
        }else {
            $resultArrayAll['state']=-1;
        }
        return $resultArrayAll;
    }
    /*
     * 获取数据条数
     */
    function get_hotel_money_count(){
        $db = new SQLBase ();
        $queryStr = 'SELECT count(*) FROM hotel_money;' ;
        $result = $db->queryData ( $queryStr );
        if (! $result || 0 == $result->num_rows) {
            $result_array_all ['state'] = - 1; // -1表示数据库出错
        } else {
            $result_array_all ['state'] = 1;
            $temp = $result->fetch_array ();
            $result_array_all ['data']=$temp['count(*)'];
        }
        return $result_array_all;
    }
    /*
     * 获取数据库指定地区数据
     */
    function getHotelMoneyData($city_ID) {
        $db = new SQLBase ();
        //判断$city_ID为几级城市
        $queryStr='SELECT LevelType FROM region where Id='.$city_ID.';' ;
        //	echo $queryStr;
        $result = $db->queryData ( $queryStr );
        if (! $result || 0 == $result->num_rows) {
            $result_array_all ['state'] = - 1; // -1表示数据库出错
        } else {
            $result_array_all ['state'] = 1;
            $temp = $result->fetch_array ();
            if($temp['LevelType']==1){
                $city_ID=substr($city_ID, 0,2);
                $queryStr='SELECT * FROM hotel_money WHERE city_ID=ANY(SELECT Id FROM region where ParentId LIKE "'.$city_ID.'%");';
                //echo $queryStr;
                $result = $db->queryData ( $queryStr );
                if (! $result || 0 == $result->num_rows) {
                    $result_array_all ['state'] = - 1; // -1表示数据库出错
                } else {
                    $result_array_all ['state'] = 1;
                    while ( $temp = $result->fetch_array () ) {
                        $result_array ['city_ID'] = $temp ['city_ID'];
                        $result_array ['city_name'] = $temp ['city_name'];
                        $result_array ['bureau_light'] = $temp ['bureau_light'];
                        $result_array ['others_light'] = $temp ['others_light'];
                        $result_array ['bureau_fire'] = $temp ['bureau_fire'];
                        $result_array ['others_fire'] = $temp ['others_fire'];
                        if($temp ['fire_time']==null||$temp ['fire_time']=='')
                            echo $temp ['fire_time']='';
                        $result_array ['fire_time'] = $temp ['fire_time'];
                        $result_array_all['data'][]=$result_array;
                    }
                }
            }
            if($temp['LevelType']==2){
                $city_ID=substr($city_ID, 0,4);
                $queryStr='SELECT * FROM hotel_money WHERE city_ID=ANY(SELECT Id FROM region where ParentId LIKE "'.$city_ID.'%");';
                $result = $db->queryData ( $queryStr );
                if (! $result || 0 == $result->num_rows) {
                    $result_array_all ['state'] = - 1; // -1表示数据库出错
                } else {
                    $result_array_all ['state'] = 1;
                    while ( $temp = $result->fetch_array () ) {
                        $result_array ['city_ID'] = $temp ['city_ID'];
                        $result_array ['city_name'] = $temp ['city_name'];
                        $result_array ['bureau_light'] = $temp ['bureau_light'];
                        $result_array ['others_light'] = $temp ['others_light'];
                        $result_array ['bureau_fire'] = $temp ['bureau_fire'];
                        $result_array ['others_fire'] = $temp ['others_fire'];
                        if($temp ['fire_time']==null||$temp ['fire_time']=='')
                            echo $temp ['fire_time']='';
                        $result_array ['fire_time'] = $temp ['fire_time'];
                        $result_array_all['data'][]=$result_array;
                    }
                }
            }
            if($temp['LevelType']==3){
                $queryStr = 'SELECT * FROM hotel_money WHERE  city_ID=' .$city_ID.';' ;
                //echo $queryStr;
                $result = $db->queryData ( $queryStr );
                if (! $result || 0 == $result->num_rows) {
                    $result_array_all ['state'] = - 1; // -1表示数据库出错
                } else {
                    $result_array_all ['state'] = 1;
                    while ( $temp = $result->fetch_array () ) {
                        $result_array ['city_ID'] = $temp ['city_ID'];
                        $result_array ['city_name'] = $temp ['city_name'];
                        $result_array ['bureau_light'] = $temp ['bureau_light'];
                        $result_array ['others_light'] = $temp ['others_light'];
                        $result_array ['bureau_fire'] = $temp ['bureau_fire'];
                        $result_array ['others_fire'] = $temp ['others_fire'];
                        if($temp ['fire_time']==null||$temp ['fire_time']=='')
                            echo $temp ['fire_time']='';
                        $result_array ['fire_time'] = $temp ['fire_time'];
                        $result_array_all['data'][]=$result_array;
                    }
                }
            }
        }
        return $result_array_all ;
    }
    /*
     * 更新数据
     */
    function updateHotelMoneyData($data){
        $db = new SQLBase ();
        $queryStr='UPDATE hotel_money Set bureau_light='.$data['bureau_light'].',';
        $queryStr.='others_light='.$data['others_light'].',';
        $queryStr.='bureau_fire='.$data['bureau_fire'].',';
        $queryStr.='others_fire='.$data['others_fire'].',';
        $queryStr.='fire_time="'.$data['fire_time'].'"';
        $queryStr.=' WHERE city_ID='.$data['city_ID'].';' ;
        //echo $queryStr;
        $result = $db->queryData ( $queryStr );
        if(!result){
            $result_array_all ['state'] = -1;
        }else{
            $result_array_all ['state'] = 1;
        }
        return $result_array_all;
    }
}